package com.support.android.myapp3.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.razerdp.widget.animatedpieview.AnimatedPieView;
import com.razerdp.widget.animatedpieview.AnimatedPieViewConfig;
import com.razerdp.widget.animatedpieview.data.SimplePieInfo;
import com.support.android.myapp3.R;
import com.support.android.myapp3.adapter.TexItemAdapter;
import com.support.android.myapp3.bean.TaxItemBean;
import com.support.android.myapp3.util.Utils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TaxCardFragment extends Fragment {
    private View view;
    private String num;
    private ListView listView;



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_tax_cardview, container, false);
        listView = (ListView) view.findViewById(R.id.taxListView);

        Bundle bundle = getArguments();
        String money = null;
        if (bundle != null) {
            money = bundle.getString("money");
        }

        if(TextUtils.isEmpty(money)){
            Toast.makeText(getActivity(), "请输入金额", Toast.LENGTH_LONG).show();
            return view;
        }

        final TaxItemBean yanglaoItem = new TaxItemBean("养老保险金","8","21",money);
        final TaxItemBean yiliaoItem = new TaxItemBean("医疗保险金","2","11",money);
        final TaxItemBean shiyeItem = new TaxItemBean("失业保险金","0.5","1.5",money);
        final TaxItemBean gongshangItem = new TaxItemBean("工伤保险金","0","0.5",money);
        final TaxItemBean shengyuItem = new TaxItemBean("生育保险金","0","1",money);
        final TaxItemBean zhufangItem = new TaxItemBean("住房公积金","7","7",money);

        List<TaxItemBean> itemList = new ArrayList<TaxItemBean>(){{
            add(yanglaoItem);
            add(yiliaoItem);
            add(shiyeItem);
            add(gongshangItem);
            add(shengyuItem);
            add(zhufangItem);
        }};

        BigDecimal pAll = new BigDecimal("0");

        for(TaxItemBean item:itemList){
            pAll = pAll.add(item.calcPersonValYuan());
        }

        BigDecimal moneyYuan = new BigDecimal(money);
        BigDecimal afterMoneyYuan = moneyYuan.subtract(pAll);
        BigDecimal personTaxYuan = calcPersonTaxYuan(afterMoneyYuan);
        BigDecimal lastMoney = afterMoneyYuan.subtract(personTaxYuan);

        List<Map<String, String>> data = calcListData(money,itemList,afterMoneyYuan,personTaxYuan,lastMoney);

//        TexItemAdapter adapter = new TexItemAdapter(getActivity(), data, R.layout.item_tax_list,
//                new String[]{"p_name", "p_person","p_company"}, new int[]{R.id.p_name, R.id.p_person,R.id.p_company});

        SimpleAdapter adapter = new SimpleAdapter(getActivity(), data, R.layout.item_tax_list,
                new String[]{"p_name", "p_person","p_company"}, new int[]{R.id.p_name, R.id.p_person,R.id.p_company});

        listView.setAdapter(adapter);

        AnimatedPieView mAnimatedPieView = view.findViewById(R.id.animatedPieView);

        float lastF = calcPer(lastMoney,moneyYuan).floatValue();
        float yanglaoF = yanglaoItem.calcPer();
        float yiliaoF = yanglaoItem.calcPer();
        float shiyeF = shiyeItem.calcPer();
        float zhufangF = zhufangItem.calcPer();

        AnimatedPieViewConfig config = new AnimatedPieViewConfig();
        config.startAngle(-90)// 起始角度偏移
                .addData(new SimplePieInfo(lastF, Utils.getColor("FF87CEFA"), lastShow(lastMoney,calcPer(lastMoney,moneyYuan))))//数据（实现IPieInfo接口的bean）
                .addData(new SimplePieInfo(yanglaoF, Utils.getColor("FFB0C4DE"), yanglaoItem.show()))
                .addData(new SimplePieInfo(yiliaoF, Utils.getColor("FFFF4500"), yiliaoItem.show()))
                .addData(new SimplePieInfo(shiyeF, Utils.getColor("FF3CB371"), shiyeItem.show()))
                .addData(new SimplePieInfo(zhufangF, Utils.getColor("FFFF8C00"), zhufangItem.show()))
                .drawText(true)
                .textSize(20)
                .duration(2000);// 持续时间

//        config.startAngle(-90)// 起始角度偏移
//                .addData(new SimplePieInfo(78, Utils.getColor("FFC5FF8C"), "这是第一段"))//数据（实现IPieInfo接口的bean）
//                .addData(new SimplePieInfo(8, Utils.getColor("FFFFD28C"), "这是第2段"))
//                .addData(new SimplePieInfo(2, Utils.getColor("FFC5FF8C"), "这是第3段"))
//                .addData(new SimplePieInfo(5, Utils.getColor("FFFFD28C"), "这是第4段"))
//                .addData(new SimplePieInfo(7, Utils.getColor("0000FF8C"), "这是第5段"))
//                .drawText(true)
//                .textSize(20)
//                .duration(2000);// 持续时间

        mAnimatedPieView.start(config);

        setListViewHeightBasedOnChildren(listView);

        Toast.makeText(getActivity(), "TaxCardFragmentShow", Toast.LENGTH_SHORT).show();
        return   view;
    }

    public List<Map<String, String>> calcListData(String money, List<TaxItemBean> itemList,
                                                  BigDecimal afterMoneyYuan,
                                                  BigDecimal personTaxYuan,
                                                  BigDecimal lastMoney) {


        List<Map<String, String>> data = new ArrayList<>();
        data.add(genItem("五险一金汇缴明细"," "," "));
        data.add(genItem("               ","个人缴纳部分","单位缴纳部分"));

        BigDecimal pAll = new BigDecimal("0");
        BigDecimal cAll = new BigDecimal("0");

        for(TaxItemBean item:itemList){
            data.add(item.genItem());
            pAll = pAll.add(item.calcPersonValYuan());
            cAll = cAll.add(item.calcCompayValYuan());
        }

        data.add(genItem("共计支出",pAll.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                cAll.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));

        data.add(genItem("个人所得税汇缴明细"," "," "));
        data.add(genItem("扣除王五险一金后月薪(应税工资)"," ",afterMoneyYuan.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));
        data.add(genItem("个人所得税"," ",personTaxYuan.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));
        data.add(genItem("税后工资"," ",lastMoney.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString()));
        return data;
    }


    public Map<String,String>genItem(String col1,String col2,String col3){
        HashMap<String, String> item = new HashMap<String, String>();
        item.put("p_name",col1);
        item.put("p_person",col2);
        item.put("p_company",col3);
        return item;
    }

    public BigDecimal calcPersonTaxYuan(BigDecimal afterBXMoney){

        int afterBxFen = afterBXMoney.multiply(new BigDecimal("100")).intValue();

        if(afterBxFen<=500000){
            return new BigDecimal(0);
        }

        int c = afterBxFen - 500000;
        BigDecimal cYuan = afterBXMoney.subtract(new BigDecimal("5000"));

        if(c <= 300000 ){
            return cYuan.multiply(new BigDecimal("0.03"));
        }

        if(c> 300000 && c <= 1200000 ){
            return cYuan.multiply(new BigDecimal("0.1")).subtract(new BigDecimal("210"));
        }

        if(c> 1200000 && c <= 2500000 ){
            return cYuan.multiply(new BigDecimal("0.2")).subtract(new BigDecimal("1410"));
        }

        if(c> 2500000 && c <= 3500000 ){
            return cYuan.multiply(new BigDecimal("0.25")).subtract(new BigDecimal("2660"));
        }

        if(c> 3500000  && c <= 5500000 ){
            return cYuan.multiply(new BigDecimal("0.30")).subtract(new BigDecimal("4410"));
        }

        if(c> 5500000 && c <= 8000000 ){
            return cYuan.multiply(new BigDecimal("0.35")).subtract(new BigDecimal("7160"));
        }

        if(c > 8000000 ){
            return cYuan.multiply(new BigDecimal("0.45")).subtract(new BigDecimal("15160"));
        }

        return new BigDecimal("0");
    }

    public BigDecimal calcPer(BigDecimal c ,BigDecimal money){
        return c.divide(money, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100"));
    }

    public String lastShow(BigDecimal money,BigDecimal per){
        return String.format("脱后月薪:比例%s%%:%s ",
                per.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(),
                money.setScale(2, BigDecimal.ROUND_HALF_DOWN).toString());
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 30 ;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = listAdapter.getView(i, null, listView);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();

        params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);
    }
}
